package com.jm.web.builder;
import java.io.IOException;
import com.jm.utils.StringBuilderEx;
import com.jm.web.BasePage;
import com.jm.web.data.BaseDataEntities;

public class TreeBuilder extends BaseBuilder{
	@Override
	public void RenderEnd() {
		String treeid = this.getBaseTag().getId();
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("<ul id=\"%1$s\" class=\"ztree\"></ul>",treeid);
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		BaseDataEntities treeData = null;
		if(this.getBaseTag().GetExtValue("treeData") instanceof BaseDataEntities){
			treeData =  (BaseDataEntities)this.getBaseTag().GetExtValue("treeData");
		}else{
			treeData = new BaseDataEntities();
		}
		
		sb.Reset();
//		sb.Append("\r\n<script>");
		sb.Append("var tree_%1$s_config={ data: {key: {title:\"tree\"}} };",treeid);
		sb.Append("$.extend(tree_%1$s_config,%2$s);",
				treeid,
				this.getBaseTag().GetExtValue("config","{}"));
		sb.Append("var tree_%1$s_nodes = %2$s;",treeid,treeData.TOJSONString());
		sb.Append("$.fn.zTree.init($(\"ul[id=%1$s]\"), tree_%1$s_config, tree_%1$s_nodes);",treeid);
//		sb.Append("</script>");
		this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY, sb.toString());
	}
}